// DlgModify.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
#include "CDataFile.h"

#include "PageDateRange.h"
#include "PageLoginInfo.h"

#include "DlgModify.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


extern CFinanceApp theApp;       

/////////////////////////////////////////////////////////////////////////////
// CDlgModify dialog


CDlgModify::CDlgModify(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgModify::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgModify)
	m_pickdate = 0;
	m_stcTips = _T("");
	//}}AFX_DATA_INIT

	m_iLstIdx = -1;
}


void CDlgModify::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgModify)
	DDX_DateTimeCtrl(pDX, IDC_DATE_ADD, m_pickdate);
	DDX_Text(pDX, IDC_TIPS, m_stcTips);
	DDX_Control(pDX, ID_UPDATE, m_btnUpdate);
	DDX_Control(pDX, IDCANCEL, m_btnCancel);
	DDX_Control(pDX, IDC_CMB_FLOW, m_cmbFlow);
	DDX_Control(pDX, IDC_CMB_PURPOSE, m_cmbPurpose);
	DDX_Control(pDX, IDC_CMB_OWNER, m_cmbOwner);
	DDX_Control(pDX, IDC_ED_COMMENT, m_edComment);
	DDX_Control(pDX, IDC_ED_MONEY, m_edAccount);
	//}}AFX_DATA_MAP

	// Make our buttons a CButtonST buttons
}


BEGIN_MESSAGE_MAP(CDlgModify, CDialog)
	//{{AFX_MSG_MAP(CDlgModify)
	ON_BN_CLICKED(ID_UPDATE, OnUpdate)
	ON_CBN_SELCHANGE(IDC_CMB_FLOW, OnSelchangeCmbFlow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgModify message handlers

BOOL CDlgModify::OnInitDialog() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL;
	CHAR			chTmp[56];
	INT				i, iCurSel;
	
	
	CDialog::OnInitDialog();
	
	// <> Init UI	
	m_btnUpdate.SetIcon(IDI_EYEON, IDI_EYEOFF);
	m_btnCancel.SetIcon(IDI_CANCEL34,IDI_CANCEL32);

	
	// <> Open RecordSet
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	m_pAccountset.CreateInstance(__uuidof(Recordset));


	// <> Open Account by passed m_iLstIdx, waiting for update
	csSQL = "SELECT * FROM Account WHERE Index=";
	sprintf( chTmp, "%ld", m_iLstIdx );
	csSQL += chTmp;
	var = csSQL;
	try
	{
		m_pAccountset->Open( var,   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}


	// <> Fill ComboBox flowclass
	try
	{
		m_pRecordset->Open( "SELECT * FROM Flowclass",   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbFlow.ResetContent();

	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return TRUE;
		}
		
		var = m_pAccountset->GetCollect("FlowClass");	// txt
		if(var.vt != VT_NULL)
			csSQL = (LPCSTR)_bstr_t(var);
	
		i = 0;
		iCurSel = 0;
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Classname"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_cmbFlow.AddString( csTmp );
			
			if( csSQL == csTmp ) 
				iCurSel = i;	// active item

			m_pRecordset->MoveNext();
			i++;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	m_cmbFlow.SetCurSel( iCurSel );

	m_pRecordset->Close();
	

	// <> Fill ComboBox Purpose
	m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );

	csSQL = "SELECT Usename FROM Purpose WHERE Flowclass='" + csTmp + "' ORDER BY Usename ASC";
	var = csSQL;
	try
	{
		m_pRecordset->Open( var,					// const _variant_t & Source
			theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbPurpose.ResetContent();
	
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return TRUE;
		}
		
		var = m_pAccountset->GetCollect("Purpose");	// txt
		if(var.vt != VT_NULL)
			csSQL = (LPCSTR)_bstr_t(var);

		i = 0;
		iCurSel = 0;
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Usename"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_cmbPurpose.AddString( csTmp );
			
			if( csSQL == csTmp ) 
				iCurSel = i;	// active item

			m_pRecordset->MoveNext();
			i++;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbPurpose.SetCurSel( iCurSel );
	
	m_pRecordset->Close();
	
	
	// <> Fill ComboBox Owner
	try
	{
		m_pRecordset->Open("SELECT * FROM Userinfo",   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbOwner.ResetContent();
	
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return TRUE;
		}
		
		var = m_pAccountset->GetCollect("Owner");	// txt
		if(var.vt != VT_NULL)
			csSQL = (LPCSTR)_bstr_t(var);

		i = 0;
		iCurSel = 0;
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Name"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_cmbOwner.AddString( csTmp );
			
			if( csSQL == csTmp ) 
				iCurSel = i;	// active item

			m_pRecordset->MoveNext();
			i++;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbOwner.SetCurSel( iCurSel );
	
	m_pRecordset->Close();
	

	// <> set edit account and comment
	m_edAccount.SetLimitText( MONEYEDIT_MAXLEN ); // order by account table
	var = m_pAccountset->GetCollect("Amount");	// nummer
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);
	else
		csTmp.Empty();
	m_edAccount.SetWindowText( csTmp );

	m_edComment.SetLimitText( 150 );	// order by account table
	var = m_pAccountset->GetCollect("Comment");	// txt
	if(var.vt != VT_NULL)
	{
		csTmp = (LPCSTR)_bstr_t(var);
		m_edComment.SetWindowText( csTmp );
	}

	// <> set current date 
	var = m_pAccountset->GetCollect("Date");		// data/time	
	if(var.vt != VT_NULL)
		csTmp = (LPCSTR)_bstr_t(var);

	// "2005/7/9" or "2005/12/31"
	m_pickdate = DTString2CTime( csTmp ); // get date from current record

	m_stcTips = "Please modify these fields to update record";
	
	UpdateData( FALSE );

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
//--------------------------------------------------------------
void CDlgModify::OnUpdate() 
{
	_variant_t		var;
	CTime			date;
	CString			csTmp;
	BOOL			bPopSetup = FALSE;
				
	
	UpdateData( TRUE );
	
	m_edAccount.GetWindowText( csTmp );
	if( csTmp.IsEmpty() )
	{
		MessageBox( "Money must be input!", "Remind", MB_ICONWARNING );
		return ;
	}

	try
	{
		// Fill fields
		var = csTmp;
		m_pAccountset->PutCollect( "Amount", var );	// Amount
		
		m_cmbPurpose.GetLBText( m_cmbPurpose.GetCurSel(), csTmp );
		var = csTmp;
		m_pAccountset->PutCollect( "Purpose", var );	// Purpose
		
		m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );
		var = csTmp;
		m_pAccountset->PutCollect( "Flowclass", var );	// Flowclass
		
		date = m_pickdate.GetTime();
		if( wtkGetSysLanguage() == WTK_BIG5 ) 
			var = date.Format( "%Y/%m/%d" );
		else
			var = date.Format( "%Y-%m-%d" );	// in GB system, is "2005-7-12"
		m_pAccountset->PutCollect( "Date", var );	// Date
		
		m_cmbOwner.GetLBText( m_cmbOwner.GetCurSel(), csTmp );
		var = csTmp;
		m_pAccountset->PutCollect( "Owner", var );	// Owner
		
		m_edComment.GetWindowText( csTmp );
		if( csTmp.IsEmpty() ) 
			var.vt = VT_NULL;
		else
			var = csTmp;	// Table Accout,field Comment must accept 0 lengh string!
		m_pAccountset->PutCollect( "Comment", var );	// Comment
		
		m_pAccountset->Update();
		
		// set OK to m_stcTips
		m_stcTips = "The current record be updated successful!";
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}


	// <> check date range
#ifdef _OUT_OF_DATE_NOTIFY_
	{
		CDataFile	MoneyDF( MONEY_CFG );
		CTime		ctBegDate, ctEndDate;	
		
		
		// <> date range type
		// <> begin date and end date
		ctBegDate = MoneyDF.GetInt( "begindate", "Date range" );
		ctEndDate = MoneyDF.GetInt( "enddate", "Date range" );
		
		if( (m_pickdate > ctEndDate) || (m_pickdate < ctBegDate) ) 
		{
			if( MessageBox( "The modified record out of the current date range!\nWould u resetup the range for listview?", "Remind", MB_ICONQUESTION|MB_YESNO ) == IDYES ) 
			{
				bPopSetup = TRUE;
			}
		}
		
	}
	
	// <> CDataFile object could not be nesting.
	if( bPopSetup ) 
	{
		CPropertySheet	propsheet( "Setup" );
		CPageDateRange	CDateRange;
		CPageLoginInfo	CLoginInfo;
		
		
		propsheet.AddPage( &CDateRange );
		propsheet.AddPage( &CLoginInfo );
		
		if( propsheet.DoModal() == IDOK ) 
		{
			;
		}
	}
#endif
	

	// send reshow 
	CDialog::OnOK();
}


// 
//--------------------------------------------------------------
CDlgModify::~CDlgModify()
{
	if( m_pRecordset != NULL )
	{
		if( m_pRecordset->State )
			m_pRecordset->Close();
		m_pRecordset = NULL;
	}
	
	if( m_pAccountset != NULL )
	{
		if( m_pAccountset->State )
			m_pAccountset->Close();
		m_pAccountset = NULL;
	}
}

// 
//--------------------------------------------------------------
void CDlgModify::OnSelchangeCmbFlow() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL;
	
	
	// <> Fill ComboBox Purpose
	m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );
	
	csSQL = "SELECT Usename FROM Purpose WHERE Flowclass='" + csTmp + "' ORDER BY Usename ASC";
	var = csSQL;
	try
	{
		m_pRecordset->Open( var,					// const _variant_t & Source
			theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbPurpose.ResetContent();
	
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return ;
		}
		
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Usename"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_cmbPurpose.AddString( csTmp );
			
			m_pRecordset->MoveNext();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_cmbPurpose.SetCurSel( 0 );
	
	m_pRecordset->Close();
	
}

void CDlgModify::SetSelIndex(INT index)
{
	m_iLstIdx = index;
}
